package com.xszx.servlet.user.address;

import com.xszx.bean.Address;
import com.xszx.dao.AddressDao;
import com.xszx.dao.OrderDao;
import com.xszx.dao.UserDao;
import com.xszx.dao.impl.AddressDaoImpl;
import com.xszx.dao.impl.OrderDaoImpl;
import com.xszx.dao.impl.UserDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/AddAddressServlet")
public class AddAddressServlet extends HttpServlet {
    static AddressDao addressDao = new AddressDaoImpl();
    static UserDao userDao = new UserDaoImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        String username = req.getSession().getAttribute("username").toString();
        if (username == null || username.equals("tourist")) {
            req.setAttribute("msg", "请先登录");
            return;
        }
        Integer userId = userDao.getUserIdByUsername(username);

        if (req.getParameter("type").equals("1")) {

        }

        String address = req.getParameter("address");
        int postalCode = Integer.parseInt(req.getParameter("postalCode"));
        String conName = req.getParameter("conName");
        String conPhone = req.getParameter("conPhone");

        if (req.getParameter("type").equals("1")) {
            int id = Integer.parseInt(req.getParameter("id"));
            Address Address = new Address(id, null, address, postalCode, conName, conPhone);
            addressDao.updateAddress(Address);
            resp.sendRedirect("BuildOrderServlet");
            return;
        }
        Address Address = new Address(null, userId, address, postalCode, conName, conPhone);
        addressDao.addAddress(Address);
        resp.sendRedirect("BuildOrderServlet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
